package com.example.baiduapi.Scoket.websocket.service;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.example.baiduapi.Alibaba.Chat.pojo.ChatRequest;
import com.example.baiduapi.Alibaba.Chat.pojo.ChatResponse;
import com.example.baiduapi.Mapper.mysqlMapper.MessageMapper;
import com.example.baiduapi.Mapper.mysqlMapper.UserMapper;
import com.example.baiduapi.Util.EncPassword;
import com.example.baiduapi.pojo.Chatmessage;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

@Service
public class ChatService {
    @Value("${Baidu.ChatApiKey}")
    String ChatApiKey;
    @Resource
    MessageMapper messagesMapper;

    public ChatResponse chat(String q) {
        String url = "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation";//这里看官方文档
        String ApiKey = ChatApiKey;//这里换成你自己的ApiKey

        ChatRequest chatRequest = new ChatRequest(q);
        String json = JSONUtil.toJsonStr(chatRequest);
        //System.out.println(json);//正式发送给api前,查看请求的主要数据情况
        String result = HttpRequest.post(url)
                .header("Authorization", "Bearer " + ApiKey)
                .header("Content-Type", "application/json")
                .body(json)
                .execute().body();
        return JSONUtil.toBean(result, ChatResponse.class);
    }

    /**
     * 添加消息记录
     *
     * @param chatmessage 添加的消息对象
     * @return
     */
    public Integer addMessage(Chatmessage chatmessage) {
        Integer addmessage = messagesMapper.addmessage(chatmessage);
        return addmessage;
    }

    public List GetInItMessage1(String User) {
        ArrayList<Object> resultList = new ArrayList<>();
        String beginTime = LocalDate.now().minusDays(31).toString();
        String endTime = LocalDate.now().plusDays(1).toString();
        List<Map<String, String>> allUser = messagesMapper.getAlluserMessagesUserByTime(User,beginTime,endTime);
        List<Map<String, String>> getAlluserMessagesByTime = messagesMapper.getAlluserMessagesByTime(User,beginTime,endTime);
        for (Map<String, String> userMap : allUser) {
            ArrayList<Object> list = new ArrayList<>();
            for (Map<String, String> MessageMap : getAlluserMessagesByTime) {
                if (MessageMap.get("getUser").equals(userMap.get("getUser"))) {
                    list.add(MessageMap.get("getUser")+"||"+EncPassword.convertMD5(MessageMap.get("message")));
                }
            }
            resultList.add("{\"data\":"+ JSON.toJSONString(list)+"}");
        }
        resultList.add("{\"name\":"+ JSON.toJSONString(allUser)+"}");
        return resultList;
    }
    public List GetInItMessage(String User) {
        ArrayList<Object> resultList = new ArrayList<>();
        String beginTime = LocalDate.now().minusDays(31).toString();
        String endTime = LocalDate.now().plusDays(1).toString();
        List<Map<String, String>> getAlluserMessagesByTime = messagesMapper.getAlluserMessagesByTime(User,beginTime,endTime);
        for (Map<String, String> messageMap : getAlluserMessagesByTime) {

            //resultList.add(JSON.toJSONString(messageMap.get("getUser")+"||"+messageMap.get("message")));

            resultList.add(JSON.toJSONString(messageMap.get("getUser")+"||"+EncPassword.convertMD5(messageMap.get("message"))));

        }
        return resultList;
    }

}
